/**
 * Created by sun on 2016/9/9.
 */



define(function (require) {
    var app = require('/../js/app');
    require('ui-table');


    var toastr =require('toastr');
    app.useModule("ui.table");

    var datepicker = require('datepicker');
    app.controller('guarantee_listCtrl', ['$scope','$http','$filter' ,function ($scope, $http,$filter) {
        $scope.ctrlScope=$scope;
        $scope.ctrlScope.itemsearch={};
        $scope.add={};
        $scope.select2Options = {
            minimumResultsForSearch: Infinity //不出现搜索框
        };
        $scope.orderParams = {
            url: "/admin/sysuser/getGuaranteeList",
            ajax:{type:2}

        };
        datepicker($scope);


        //搜索
        $scope.search = function() {
            $scope.ctrlScope.itemsearch.beginDate=$scope.ctrlScope.itemsearch.beginDatex;
            $scope.ctrlScope.itemsearch.endDate= $scope.ctrlScope.itemsearch.endDatex;
            $scope.ctrlScope.itemsearch.beginDate=$filter('date')($scope.ctrlScope.itemsearch.beginDate,'yyyy-MM-dd');
            $scope.ctrlScope.itemsearch.endDate=$filter('date')($scope.ctrlScope.itemsearch.endDate,'yyyy-MM-dd');
            $scope.ctrlScope.itemsearch.type=2;
            $scope.orderParams.ajaxParam=$scope.ctrlScope.itemsearch;

            $scope.orderParams.reload();
        };

        //全局查询重置
        $scope.reset=function(){
            $scope.ctrlScope.itemsearch={};
            $scope.orderParams.ajaxParam={type:'2'};
            $scope.orderParams.reload();

        };

        //新建用户
        $scope.addOrderModal=function(){
            $scope.add={};
            $scope.addModal = !$scope.addModal ;
        };
        //创建提交
        $scope.addSubmit = function(isValid) {
            if (!isValid) {
                toastr.error("信息填写有误");
            }

            $http({
                method: 'POST',
                url: "/admin/sysuser/add",
                data:{
                    type:2,
                    sysUserName:$scope.add.sysUserName,
                    password:$scope.add.password
                }
            }).success(function(data) {
                console.log(data)

                if(data.result=="success"){
                    toastr.success("创建成功！");
                    $scope.addModal = !$scope.addModal;
                    $scope.orderParams.ajaxParam={type:'2'};
                    $scope.orderParams.reload();
                }else{
                    if(data.messageText){
                        toastr.error(data.messageText);
                    }else{
                        toastr.error('创建失败');
                    }
                }

            }).error(function(data) {
            });
        };

        //认证
        $scope.identify=function(id){
            $scope.identifyModel = !$scope.identifyModel ;
            $scope.add1={};
            $scope.add1.userId=id;
        };
        //修改
        $scope.modify=function(id){
            $scope.modifyModal = !$scope.modifyModal ;
            $scope.add1={};
            $http({
                method: 'POST',
                url: "/admin/user/getGuaranteeSecurityByUserId",
                data: {
                    userId:id
                }
            }).success(function(data) {
                console.info(data);
                $scope.add1 = data;
                $scope.add1.enterpriseFullName=data.enterpriseFullName;
            }).error(function(data) {
            });
        };

        //跳转先锋页面
        var SERVERHOST = window.location.protocol +"//"+window.location.host;
        $scope.xfInformation = function(id) {
            window.open(SERVERHOST+'/admin/user/xfInformation?id='+id);
        };

        //认证提交
        $scope.identifySubmit = function(isValid) {
            console.log($scope.add1);
            $scope.add1.bizType = '03';
            if (!isValid) {
                toastr.error("信息填写有误");
            }
            $http({
                method: 'POST',
                url: "/admin/sysUser/guaranteeCertification",
                data:$scope.add1
            }).success(function(data) {
                console.log(data)
                if(data.result=="success"){
                    toastr.success("认证成功！");
                    $scope.identifyModel = !$scope.identifyModel;
                    $scope.orderParams.ajaxParam={type:'2'};
                    $scope.orderParams.reload();
                }else{
                    if(data.messageText){
                        toastr.error(data.messageText);
                    }else{
                        toastr.error("认证失败");
                    }

                }

            }).error(function(data) {
            });
        };

        //修改提交
        $scope.modifySubmit = function(isValid) {
            console.log($scope.add1);
            $scope.add1.bizType = '03';
            if (!isValid) {
                toastr.error("信息填写有误");
            }
            $http({
                method: 'POST',
                url: "/admin/sysUser/updateEnterprise",
                data:$scope.add1
            }).success(function(data) {
                console.log(data)
                if(data.result=="success"){
                    toastr.success("修改成功！");
                    $scope.modifyModal = !$scope.modifyModal;
                    $scope.orderParams.ajaxParam={type:'2'};
                    $scope.orderParams.reload();
                }else{
                    if(data.messageText){
                        toastr.error(data.messageText);
                    }else{
                        toastr.error("修改失败");
                    }

                }

            }).error(function(data) {
            });
        };

    }]);


});